package com.st.conroller;

import com.st.entity.Result;
import com.st.entity.UserDto;
import com.st.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class LoginController {
    @Autowired
    private IUserService iUserService;
    //账号登录
    @RequestMapping("/login")
    public Result login(@RequestBody UserDto userDto){
        Result result = iUserService.login(userDto);
        return result;
    }
    //手机验证码登录
    @RequestMapping("/codeLogin")
    public Result codeLogin(@RequestBody UserDto userDto){
        return iUserService.codeLogin(userDto);
    }
    //发送短信验证码
    @RequestMapping("/sendCode")
    public Result sendCode(String phone){
        return iUserService.sendCode(phone);
    }
    //发送图形验证码
    @RequestMapping("/sendPigCode")
    public void sendPigCode(HttpServletResponse response) throws IOException {
        Result result = iUserService.sendPigCode();
        if (result.isSuccess()){
//            BufferedImage image = ImageCodeUtil.getBufferedImage();
            BufferedImage image = (BufferedImage) result.getData();
            ImageIO.write(image,"jpg",response.getOutputStream());
        }
    }
    //验证图形验证码
    @RequestMapping("/verifyCode")
    public Result verifyCode(String code) {
        return iUserService.verifyCode(code);
    }
    //校验token
     @RequestMapping("check")
     public Result check(@RequestHeader("token") String token){
        return iUserService.check(token);
        }

}
